package TTEST;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class Logi  {
    public static Map<String,String> aaa=new HashMap<String,String>();
    
    public  Map<String, String> getAaa() {
            String listString="";
            for (String s : mm.get("aa"))
            {
                listString += s + "\n";
            }
            aaa.put(zmiana, listString);
            System.out.println("aaa="+aaa);
            return aaa;
    }
    public String zmiana="ccc";
    
    public String getZmiana() {
            return zmiana;
    }
    public void setZmiana(String zmiana) {
            this.zmiana = zmiana;
    }
    public void zmiana2(){
            zmiana+=1;
            /*for (int i = 0; i < 30; i++) {
                    zmiana=String.valueOf(i);
            try
            {
                    Thread.sleep(1000);
            }
            catch(Exception e){}
            }*/
    }
    public void zmiana()
    {
    }
    public void setAaa(Map<String, String> aaa) {
            Logi.aaa = aaa;
    }
    private int cc;private String test;
    
    public String getTest() {
            return test;
    }
    static void gg(String ... g){
    	 PrintWriter out = null;
    	  BufferedWriter bw = null;
    	  FileWriter fw = null;
    	  try{
    	     fw = new FileWriter(g[0]+".txt", true);
    	     bw = new BufferedWriter(fw);
    	     out = new PrintWriter(bw);
    	     out.println(g[1]);
    	  }
    	  catch( IOException e ){
    	     // File writing/opening failed at some stage.
    	  }
    	  finally{
    	     try{
    	        if( out != null ){
    	           out.close(); // Will close bw and fw too
    	        }
    	        else if( bw != null ){
    	           bw.close(); // Will close fw too
    	        }
    	        else if( fw != null ){
    	           fw.close();
    	        }
    	        else{
    	           // Oh boy did it fail hard! :3
    	        }
    	     }
    	     catch( IOException e ){
    	        // Closing the file writers failed for some obscure reason
    	     }
    	  }
/*             try{
                      FileWriter fstream = new FileWriter(g[0]+".txt");
                      BufferedWriter out = new BufferedWriter(fstream);
                      out.write(g[1]);
                      out.close();
                      }catch (Exception e){
                      System.err.println("Error: " + e.getMessage());
                      }*/
    }
    public void setTest(String test) {
            this.test = test;
    }
    public static void dodawanie(String ... b){
                    gg(b[0],b[1]);
    }
    public static Map<String,List<String>> mm=new HashMap<String,List<String>>();
    static{
            List<String> a=new ArrayList<String>();
            /*for (int i = 0; i < 20; i++) {
                    try{
                    Thread.sleep(2000);
                    }
                    catch(Exception e) {}
                    a.add("a"+i);
                    mm.put("aa", a);
            }*/
            mm.put("aa", a);
            aaa.put("ccc","to jest \n wartosc\n nowa");
            aaa.put("bbb","to jest \n wartosc\n nowass\ncccccccccccccccc");
            List<String> b=new ArrayList<String>();
            for (int i = 0; i < 20; i++) {
                    b.add("b"+i);
            }
            mm.put("bb", b);
    }
    private static Logi instancja=new Logi();
    
    public  Map<String, List<String>> getMm() {
            return mm;
    }
    public  void setMm(Map<String, List<String>> mm) {
            Logi.mm = mm;
    }
    public static Logi getInstance()
    {
            if(instancja==null)
                    return instancja=new Logi();
            else
                    return instancja;
    }
    public static void dopisz(String IP_Port,String h){
            List<String> d=mm.get(IP_Port);
            System.out.println("h=="+h);
            d.add(h);
    //      System.out.println("d="+d);
            mm.put(IP_Port,d);
            String a=Logi.getInstance().getZmiana();
            a+=h;
            Logi.getInstance().setZmiana(a);
            //System.out.println("dopisano");
    }
    private static String doStringa(List<String> lista){
            String temp="";
            for (Iterator iterator = lista.iterator(); iterator.hasNext();) {
                    temp+=(String) iterator.next();
            }
            return temp;
    }
    public static void nowelogi(String IP_Port)
    {
            List<String> nowelogi=new ArrayList<String>();
            //aaa.put(key, value)
            mm.put(IP_Port, nowelogi);
            Logi.getInstance().setZmiana(doStringa(nowelogi));
    }
    
    
}
